package messagepasser;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import realtime.SendDocumentState;

public class Document {
	int docId;
	UserInfo myself;
	MessagePasser myPasser;
	List<UserInfo> users = new ArrayList<UserInfo>();
	SendDocumentState docState;
	public JPanel panel;
	String docName;
	String content;
	public JTextArea contentBox;

	
	public JTextArea getContentBox() {
		return contentBox;
	}


	public void setContentBox(JTextArea contentBox) {
		this.contentBox = contentBox;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}


	public JPanel getPanel() {
		return panel;
	}


	public void setPanel(JPanel panel) {
		this.panel = panel;
	}


	public Document (int docId, UserInfo myself, List<UserInfo> allUsers, DocPermissions[] docUsers, String name) {
		
		this.docId = docId;
		this.myself = myself;
		docName = name;
		
		for (DocPermissions d : docUsers) {
			
			for (UserInfo u : allUsers) {
			//	System.out.println ("herer" + u.userid + d.userid + d.docid);
				if (u.getUserid() == d.userid) {
				//	System.out.println ("herer" + d.userid);
					users.add(u);
				}
			}
		}
		ClockFactory factory = new ClockFactory();
		ClockService clock = factory.getInstance("Vector");
	//	System.out.println ("Users " + allUsers.length);
		myPasser = new MessagePasser(users, myself, clock);
		content = "yes";
	    Timer t = new Timer();
		
		docState = new SendDocumentState(this, t);
		t.schedule(docState, 10000, 1000);
	}


	public String getDocName() {
		return docName;
	}


	public void setDocName(String docName) {
		this.docName = docName;
	}


	public int getDocId() {
		return docId;
	}


	public void setDocId(int docId) {
		this.docId = docId;
	}


	public UserInfo getMyself() {
		return myself;
	}


	public void setMyself(UserInfo myself) {
		this.myself = myself;
	}


	public MessagePasser getMyPasser() {
		return myPasser;
	}


	public void setMyPasser(MessagePasser myPasser) {
		this.myPasser = myPasser;
	}


	public List<UserInfo> getUsers() {
		return users;
	}


	public void setUsers(List<UserInfo> users) {
		this.users = users;
	}


	public SendDocumentState getDocState() {
		return docState;
	}


	public void setDocState(SendDocumentState docState) {
		this.docState = docState;
	}
}
